# -*- encoding=utf8 -*-
__author__ = "hyq"

import mywx
from helium import *
import time
from datetime import datetime, timedelta

worktime = {
    'w1': [],
    'w2': [('00:00', '0:30'), ('4:00', '4:30'), ('11:50', '12:50'), ('17:00', '18:00'), ('23:40', '23:59')],
    'w4': [('00:00', '0:30'), ('4:00', '4:30'), ('11:50', '12:50'), ('17:00', '18:00'), ('23:40', '23:59')],
    'w5': [('00:00', '8:00'), ('11:45', '12:40'), ('17:00', '18:00'), ('20:30', '23:59')],
    'w6': [('00:00', '8:00'), ('11:45', '12:40'), ('17:00', '18:00'), ('20:30', '23:59')],
    'w7': [('00:00', '0:40'), ('4:30', '5:00'), ('12:00', '12:50'), ('17:25', '18:20'), ('23:50', '23:59')],
    'w9': [],
    'w10': [],
    'r290': []
}


def intime(line, worktime):
    '''
    确认一条线是否休息
    :param line: 线体名称
    :param worktime: 记录休息时间的字典
    :return: 如果上班时间，则返回True，休息则返回False
    '''
    # start like '9:30'
    list = worktime[line]
    if list:
        for every_time in list:
            # 在list中取得休息的开始时间，和结束时间
            start = every_time[0]
            end = every_time[1]
            starttime = datetime.strptime(str(datetime.now().date()) + start, '%Y-%m-%d%H:%M')
            endtime = datetime.strptime(str(datetime.now().date()) + end, '%Y-%m-%d%H:%M')
            Dtime = datetime.now() - timedelta(minutes=20)
            # 当在休息时间段内，则返回False
            if Dtime > starttime and Dtime < endtime:
                return False
        # 遍历完都不是，则是在工作时间，返回True
        return True
    else:
        # 空白则返回False
        False


def send(message_dict, line):
    if line in ['w1', 'w2', 'w4', 'w5', 'w6']:
        print('sending weixin~~~~', message_dict[line])
        mywx.sendText('外二车间急停分析结果', message_dict[line])
    elif line in ['w7', 'w9', 'w10', 'r290']:
        mywx.sendText('外一车间急停分析结果', message_dict[line])
        print('sending weixin~~~~', message_dict[line])


user = {'username': 'huangyongqin', 'password': 'yongs81~'}


def send_mail_by_hx(title, emails, file=None):
    start_chrome('mail.hisense.com', headless=True)
    write(user['username'], into="用户名")
    time.sleep(1)
    write(user['password'], into='密　码')
    click('登录')
    try:
        click('写 信')
        # 输入邮箱地址
        click(S('ul', to_right_of='收件人'))
        for email in emails:
            write(email)
            press(ENTER)
        time.sleep(1)
        # 输入抄送地址
        press(TAB)
        write('huangyongqin@hisense.com')
        press(ENTER)
        time.sleep(1)
        # 输入主题
        press(TAB)
        write(title)
        time.sleep(1)
        if len(file) > 1:
            for f in file:
                drag_file(f, to='将文件拖放到此，可作为附件添加')
        elif len(file) == 1:
            drag_file(file, to='将文件拖放到此，可作为附件添加')
            time.sleep(1)
        click('发 送')
        if Text('确定不需要写 邮件内容 吗？').exists():
            click('确定')
        time.sleep(1)
        if Text('邮件已发送').exists():
            return True
        else:
            return False
    except:
        return False
    finally:
        kill_browser()



if __name__ == '__main__':
    send('{0}{1}{2}短暂停通告：{3}在过去10分钟内急停最多，有{4}次，请线长到现场处理。'.format('W10', '白班', '3月8日', '卤检急停', '11'))
